package cn.shyouchi.wuguanjia.android.ui.event.temporary;

import android.content.Intent;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;

import org.greenrobot.eventbus.EventBus;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import cn.shyouchi.wuguanjia.android.R;
import cn.shyouchi.wuguanjia.android.api.event.EventApi;
import cn.shyouchi.wuguanjia.android.base.BaseActivity;
import cn.shyouchi.wuguanjia.android.event.DataChangeEvent;
import cn.shyouchi.wuguanjia.android.model.BaseRequestResult;
import cn.shyouchi.wuguanjia.android.subscribers.ProgressSubscriber;
import cn.shyouchi.wuguanjia.android.widget.TopBar;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

public class PreviewTemporaryActivity extends BaseActivity {


    @BindView(R.id.topBar)
    TopBar mTopBar;
    @BindView(R.id.edit_activity_name)
    TextView mEditActivityName;
    @BindView(R.id.edit_activity_position)
    TextView mEditActivityPosition;
    @BindView(R.id.edit_activity_person)
    TextView mEditActivityPerson;
    @BindView(R.id.edit_activity_phone)
    TextView mEditActivityPhone;
    @BindView(R.id.textView4)
    TextView mTextView4;
    @BindView(R.id.tv_start_time)
    TextView mTvStartTime;
    @BindView(R.id.tv_end_time)
    TextView mTvEndTime;
    @BindView(R.id.edit_activity_content)
    TextView mEditActivityContent;
    @BindView(R.id.btn_del)
    Button mBtnDel;
    @BindView(R.id.btn_edit)
    Button mBtnEdit;
    @BindView(R.id.activity_add_handover)
    LinearLayout mActivityAddHandover;

    @BindView(R.id.btn_group)
    LinearLayout btn_group;

    TemporaryEntity.ResultMessageBean entity;
    String projectId;
    @BindView(R.id.edit_activity_person_count)
    TextView mEditActivityPersonCount;
    @BindView(R.id.edit_activity_time)
    TextView mEditActivityTime;
    @BindView(R.id.tv_activity_start_time)
    TextView mTvActivityStartTime;
    @BindView(R.id.tv_activity_end_time)
    TextView mTvActivityEndTime;
    @BindView(R.id.edit_com_name)
    TextView mEditComName;
    @BindView(R.id.edit_com_person)
    TextView mEditComPerson;
    @BindView(R.id.edit_com_phone)
    TextView mEditComPhone;


    @Override
    public int initContentView() {
        return R.layout.activity_preview_temporary;
    }

    @Override
    public void initInjector() {
        projectId = getIntent().getStringExtra("projectId");
        entity = getIntent().getParcelableExtra("temporary");
    }

    @Override
    public void initUiAndListener() {
        ButterKnife.bind(this);
        mTopBar.setTitle(getTitle().toString());
        mTopBar.setLeftIcon(R.mipmap.ic_back);
        setSupportActionBar(mTopBar.getToolbar());

        mEditActivityPosition.setText(entity.getAddress());

        mEditActivityName.setText(entity.getProjectname());
        mEditActivityPerson.setText(entity.getProjectleader());
        mEditActivityPersonCount.setText(String.valueOf(entity.getNumber()));
        mEditActivityTime.setText(String.format("%s个小时", entity.getTime()));
        mEditActivityPhone.setText(entity.getPhone());
        mEditActivityContent.setText(entity.getDescription());
        mTvStartTime.setText(entity.getStartingtime());
        mTvEndTime.setText(entity.getEndtime());
        mTvActivityStartTime.setText(entity.getStartingtime());
        mTvActivityEndTime.setText(entity.getEndtime());

        mEditComName.setText(entity.getOwner_company());
        mEditComPerson.setText(entity.getOwner_contacts());
        mEditComPhone.setText(entity.getOwner_phone());


        if (TextUtils.isEmpty(projectId)) {
            btn_group.setVisibility(View.GONE);
        }

    }

    @Override
    protected boolean isApplyStatusBarTranslucency() {
        return false;
    }

    @Override
    protected boolean isApplyStatusBarColor() {
        return true;
    }


    @OnClick({R.id.btn_del, R.id.btn_edit})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.btn_del:
                EventApi eventApi = new EventApi(mContext);
                eventApi.delTemporary(String.valueOf(entity.getId()))
                        .subscribeOn(Schedulers.io())
                        .observeOn(AndroidSchedulers.mainThread())
                        .subscribe(new ProgressSubscriber<BaseRequestResult>(mContext) {
                            @Override
                            public void onNext(BaseRequestResult result) {
                                EventBus.getDefault().post(new DataChangeEvent());
                                finish();
                            }
                        });

                break;
            case R.id.btn_edit:
                Intent intent = new Intent(mContext, AddTemporaryActivity.class);
                intent.putExtra("temporary", entity);
                intent.putExtra("projectId", projectId);
                startActivity(intent);
                finish();
                break;
        }
    }

}
